package com.nhim.cms.admin.client.template.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.nhim.cms.admin.client.rpc.TemplateRPCService;
import com.nhim.cms.admin.client.rpc.TemplateRPCServiceAsync;
import com.nhim.cms.admin.shared.ETemplateType;
import com.nhim.cms.admin.shared.TemplateRPCProxy;

public class TemplateOverview extends Composite {

	private static TemplateOvewviewUiBinder uiBinder = GWT
			.create(TemplateOvewviewUiBinder.class);

	interface TemplateOvewviewUiBinder extends
			UiBinder<Widget, TemplateOverview> {
	}

	public TemplateOverview() {
		initWidget(uiBinder.createAndBindUi(this));

	}

	private final TemplateRPCServiceAsync service = GWT
			.create(TemplateRPCService.class);
	TemplateRPCProxy template;
	ETemplateType type;
	Integer contentTypeId;

	public void Display(TemplateRPCProxy template, ETemplateType type,
			Integer contentTypeId) {
		try {
			this.template = template;
			this.contentTypeId = contentTypeId;
			this.type = type;
			if (template != null)
				txtTemplateName.setText(template.getName());
			else
				txtTemplateName.setText("");

			switch (type) {
			case GROUP_TEMPLATE:
				ShowHideGroupTemplate(false);
				ShowHideSiteTemplate(true);
				BindSiteTemplate();
				break;
			case SITE_TEMPLATE:
				ShowHideGroupTemplate(false);
				ShowHideSiteTemplate(false);
				break;
			case CONTENT_TEMPLATE:
				ShowHideGroupTemplate(true);
				ShowHideSiteTemplate(true);
				BindGroupTemplate();
				BindSiteTemplate();
				break;
			}
		} catch (Exception ex) {
			Window.alert(ex.getMessage());
		}
	}

	private void BindSiteTemplate() {
		AsyncCallback<List<TemplateRPCProxy>> asyncResult = new AsyncCallback<List<TemplateRPCProxy>>() {

			@Override
			public void onFailure(Throwable error) {
				Window.alert(error.getMessage());

			}

			@Override
			public void onSuccess(List<TemplateRPCProxy> result) {
				cbSiteTemplate.clear();
				cbSiteTemplate.addItem(" ", "0");
				for (TemplateRPCProxy proxy : result) {
					cbSiteTemplate.addItem(proxy.getName(), proxy.getId()
							.toString());
					if(proxy.getId() == template.getSiteTemplateId())
						cbSiteTemplate.setSelectedIndex(cbSiteTemplate.getItemCount()-1);
					
				}

			}
		};
		service.findAllSiteTemplate(asyncResult);
		if (template == null)
			cbSiteTemplate.setSelectedIndex(0);
		
	}

	private void BindGroupTemplate() {
		AsyncCallback<List<TemplateRPCProxy>> asyncResult = new AsyncCallback<List<TemplateRPCProxy>>() {

			@Override
			public void onFailure(Throwable error) {
				Window.alert(error.getMessage());

			}

			@Override
			public void onSuccess(List<TemplateRPCProxy> result) {
				cbGroupTemplate.clear();
				cbGroupTemplate.addItem(" ", "0");
				for (TemplateRPCProxy proxy : result) {
					cbGroupTemplate.addItem(proxy.getName(), proxy
							.getId().toString());
					if(proxy.getId() == template.getGroupTemplateId())
						cbGroupTemplate.setSelectedIndex(cbGroupTemplate.getItemCount()-1);
				}

			}
		};

		service.findAllGroupTemplate(asyncResult);
		if (template == null)
			cbGroupTemplate.setSelectedIndex(0);
		
	}

	private void ShowHideGroupTemplate(Boolean show) {
		cbGroupTemplate.setVisible(show);
		lblGroupTemplate.setVisible(show);

	}

	private void ShowHideSiteTemplate(Boolean show) {
		cbSiteTemplate.setVisible(show);
		lblSiteTemplate.setVisible(show);
	}

	public void save() {
		if (template == null)
			template = new TemplateRPCProxy(txtTemplateName.getText(),
					type.getCode());
		if ("".equals(txtTemplateName.getText().trim())) {
			Window.alert("Template name is required.");
			return;
		}

		template.setName(txtTemplateName.getText());
		switch (type) {
		case GROUP_TEMPLATE:
			template.setSiteTemplateId(cbSiteTemplate.getSelectedIndex() != 0 ? Integer
					.parseInt(cbSiteTemplate.getValue(cbSiteTemplate
							.getSelectedIndex())) : 0);
			break;
		case SITE_TEMPLATE:
			break;
		case CONTENT_TEMPLATE:
			template.setGroupTemplateId(cbGroupTemplate.getSelectedIndex() != 0 ? Integer
					.parseInt(cbGroupTemplate.getValue(cbGroupTemplate
							.getSelectedIndex())) : 0);
			template.setSiteTemplateId(cbSiteTemplate.getSelectedIndex() != 0 ? Integer
					.parseInt(cbSiteTemplate.getValue(cbSiteTemplate
							.getSelectedIndex())) : 0);
			template.setContentTypeId(contentTypeId);
			break;
		}
		AsyncCallback<String> asyncResult = new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable error) {
				Window.alert(error.getMessage());

			}

			@Override
			public void onSuccess(String validation) {
				if (validation.startsWith("succeeded")) {
					template.setId(Integer.parseInt(validation.trim()
							.split("!")[1]));
					listener.onCompletedSave(template);
				} else
					Window.alert(validation);

			}
		};
		service.persitTemplate(template, asyncResult);

	}

	@UiField
	TextBox txtTemplateName;

	@UiField
	Label lblSiteTemplate;

	@UiField
	Label lblGroupTemplate;

	@UiField
	ListBox cbSiteTemplate;

	@UiField
	ListBox cbGroupTemplate;
	
	private ComplatedSaveListener listener;
	/**
	 * Custom listener for this widget.
	 */
	public interface ComplatedSaveListener {

		/**
		 * Called when the user save Template
		 * 
		 */
		void onCompletedSave(TemplateRPCProxy template);
	}
	public void setListener(ComplatedSaveListener listener) {
		this.listener = listener;
	}


}
